home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1998 October / Macworld (1998-10).dmg / Games World / Hot New Games / Real Pool Demo / DATA / NET.DAT / 00015_Network Code.ls < prev    next >
Encoding:
Text File  |  1998-05-01  |  6.1 KB  |  228 lines

  1. global clientCookie, netPlayer, gamePort, gbuffer, ip, connect, identity, netcookie, name, name2
  2.  
  3. on initnet
  4.   if objectp(xtra("xtranet")) = 0 then
  5.     if the machineType = 256 then
  6.       openXLib("Xtranet.x32")
  7.     else
  8.       openXLib("Xtranet‚Ñ¢")
  9.     end if
  10.   end if
  11.   XNetInit("XNET100-84731-01878")
  12.   set ip to XNetIPAddress()
  13.   set gamePort to 6001
  14.   put "-IP address=" & ip & "-" & RETURN into field "chat"
  15.   set connect to 0
  16.   set identity to 0
  17.   set clientCookie to -1
  18.   set gbuffer to EMPTY
  19.   startconnect()
  20. end
  21.  
  22. on onlineloop
  23.   if connect = 0 then
  24.     readyconnect()
  25.   else
  26.     if gbuffer = VOID then
  27.       set gbuffer to EMPTY
  28.     end if
  29.     set Lincoming to readdata()
  30.     put string(Lincoming) after gbuffer
  31.     if char length(gbuffer) of gbuffer = "|" then
  32.       doproc()
  33.     end if
  34.   end if
  35. end
  36.  
  37. on senddata n
  38.   if clientCookie < 0 then
  39.     exit
  40.   end if
  41.   XNetPeerSend(clientCookie, n & "|")
  42. end
  43.  
  44. on readdata
  45.   if clientCookie < 0 then
  46.     exit
  47.   end if
  48.   set ret to EMPTY
  49.   set ret to XNetPeerGet(clientCookie)
  50.   return ret
  51. end
  52.  
  53. on connect serverName
  54.   if the paramCount = 0 then
  55.     set serverName to field "ip"
  56.   end if
  57.   set msg to "Contacting host" && serverName & "..."
  58.   put msg & RETURN after field "chat"
  59.   updateStage()
  60.   set netPlayer to 0
  61.   set netcookie to XNetPeerCreate()
  62.   if netcookie > 0 then
  63.     set netError to -1
  64.     startTimer()
  65.     repeat while (netError <> 0) and (the timer < (7 * 60))
  66.       set netError to XNetPeerConnect(netcookie, serverName, gamePort)
  67.     end repeat
  68.     if netError <> 0 then
  69.       set msg to "Error connecting with remote player: " & XNetError(netError)
  70.       put msg & RETURN after field "chat"
  71.       XNetClose(netcookie)
  72.     else
  73.       set msg to "Establishing communications..."
  74.       put msg & RETURN after field "chat"
  75.       updateStage()
  76.       set sendString to "Here she comes..."
  77.       put "Sending: " & sendString
  78.       XNetPeerSend(netcookie, sendString)
  79.       set receiveString to EMPTY
  80.       startTimer()
  81.       repeat while (receiveString = EMPTY) and (the timer < (15 * 60))
  82.         set receiveString to XNetPeerGet(netcookie)
  83.       end repeat
  84.       if not stringp(receiveString) then
  85.         connectFail(receiveString)
  86.       else
  87.         if receiveString <> EMPTY then
  88.           put "Received: " & receiveString
  89.           put "Connection verified!"
  90.           set netPlayer to 1
  91.           set connect to 1
  92.           set identity to 1
  93.           set msg to "bye " & the text of field "name"
  94.           XNetClose(clientCookie)
  95.           set clientCookie to netcookie
  96.           set name to field "name"
  97.           if (name = " ") or (name = EMPTY) then
  98.             set name to "<anonymous>"
  99.           end if
  100.           set ss to "[connection established with: " & name & "]"
  101.           send(ss)
  102.           puppetSound(11)
  103.           set ss to string("set name2=" && QUOTE & string(name) & QUOTE)
  104.           senddata(ss)
  105.           go(1)
  106.           if not (the commandDown) then
  107.             closeWindow()
  108.           end if
  109.         else
  110.           set msg to "Connection failed!"
  111.           put msg & RETURN after field "chat"
  112.           connectFail(0)
  113.         end if
  114.       end if
  115.     end if
  116.   else
  117.     set msg to "Error connecting with remote player: " & netcookie
  118.     put msg & RETURN after field "chat"
  119.     connectFail()
  120.   end if
  121. end
  122.  
  123. on startconnect
  124.   set msg to "Waiting for other player to connect..."
  125.   put msg & RETURN after field "chat"
  126.   set lineCnt to the number of lines in field "chat"
  127.   updateStage()
  128.   set netPlayer to 0
  129.   set connect to 0
  130.   set netcookie to XNetPeerCreate()
  131. end
  132.  
  133. on readyconnect
  134.   startTimer()
  135.   if netcookie > 0 then
  136.     set netError to -1
  137.     if testconnect() then
  138.       if the mouseDown = 1 then
  139.         connect()
  140.       end if
  141.       exit
  142.     end if
  143.     repeat while (bgAllKeys() = []) and (the mouseDown = 0) and (netError <> 0)
  144.       set netError to XNetPeerAccept(netcookie, gamePort)
  145.     end repeat
  146.     if netError <> 0 then
  147.     else
  148.       set receiveString to EMPTY
  149.       startTimer()
  150.       repeat while (receiveString = EMPTY) and (the timer < (15 * 60))
  151.         set receiveString to XNetPeerGet(netcookie)
  152.       end repeat
  153.       if not stringp(receiveString) then
  154.         connectFail(receiveString)
  155.       end if
  156.       if receiveString <> EMPTY then
  157.         set connect to 1
  158.         put "Received: " & receiveString
  159.         set sendString to "...and there she goes."
  160.         put "Sending: " & sendString
  161.         XNetPeerSend(netcookie, sendString)
  162.         put "Connection verified!"
  163.         set netPlayer to 2
  164.         set identity to 2
  165.         set msg to "bye "
  166.         set err to XNetPeerSend(clientCookie, msg)
  167.         XNetClose(clientCookie)
  168.         set clientCookie to netcookie
  169.         set name to field "name"
  170.         if (name = " ") or (name = EMPTY) then
  171.           set name to "<anonymous>"
  172.         end if
  173.         set ss to "[connection established with: " & name & "]"
  174.         send(ss)
  175.         puppetSound(11)
  176.         set ss to string("set name2=" && QUOTE & name & QUOTE)
  177.         senddata(ss)
  178.         closeWindow()
  179.         tell the stage
  180.           initchat()
  181.         end tell
  182.       else
  183.         set msg to "Error connecting with remote player: " & netError
  184.         put msg & RETURN after field "chat"
  185.         connectFail("timeout waiting for response")
  186.       end if
  187.     end if
  188.   else
  189.     set msg to "Error connecting with remote player: " & netcookie
  190.     put msg & RETURN after field "chat"
  191.     connectFail(netcookie)
  192.     set netcookie to -1
  193.   end if
  194. end
  195.  
  196. on testconnect
  197.   return inside(point(the mouseH, the mouseV), rect(203, 41, 263, 53))
  198. end
  199.  
  200. on waitReply cookie, secondsToWait
  201.   set timeToWait to secondsToWait * 60
  202.   set msg to EMPTY
  203.   startTimer()
  204.   repeat while (msg = EMPTY) and (the timer < timeToWait)
  205.     set msg to XNetPeerGet(cookie)
  206.   end repeat
  207.   if not stringp(msg) then
  208.     connectFail(msg)
  209.   end if
  210.   return msg
  211. end
  212.  
  213. on connectFail theErr
  214.   alert("Connection failed!" & RETURN & "Error code:" && theErr)
  215.   set err to XNetPeerSend(clientCookie, "bye")
  216.   XNetClose(clientCookie)
  217.   set clientCookie to 0
  218.   abort()
  219.   set the text of field "msg" to " "
  220.   set the editable of member "myname" to 1
  221.   set the editable of member "server" to 1
  222.   repeat with i = 1 to 48
  223.     puppetSprite(i, 0)
  224.   end repeat
  225.   go(1)
  226.   abort()
  227. end
  228.